package cn.edu.nchu.fragmentdemo.customcomponents;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import cn.edu.nchu.fragmentdemo.R;

/**
 * @className: CustomSeekbarWithText
 * @description: 自定义组合组件
 * @author: Kuangkeli
 * @date: 2023/09/21 9:54
 **/
public class UavCustomSeekbarWithCheckbox extends LinearLayout {

    private SeekBar seekBar;
    private TextView titleTextView;
    private CheckBox checkBox;
    private TextView processTextView;
    CompoundButton.OnCheckedChangeListener checkChangeListener;

    public UavCustomSeekbarWithCheckbox(Context context) {
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public UavCustomSeekbarWithCheckbox(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SeekbarWithCheckBox);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.customcomponent_uav_seekbarwithcheckbox, this);
        seekBar = findViewById(R.id.seekbarWithCheckBox_seekBar);
        seekBar.setEnabled(false);
        seekBar.setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_progress_follow));
        seekBar.setThumb(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_thumb_follow));
        titleTextView = findViewById(R.id.seekbarWithCheckBox_title_textview);
        checkBox = findViewById(R.id.seekbarWithCheckBox_checkbox);
        processTextView = findViewById(R.id.seekbarWithCheckBox_progress_textview);
        titleTextView.setText(ta.getText(R.styleable.SeekbarWithCheckBox_seekbarWithCheckBox_title));
        seekBar.setMin(ta.getInteger(R.styleable.SeekbarWithCheckBox_seekbarWithCheckBox_min, 0));
        seekBar.setMax(ta.getInteger(R.styleable.SeekbarWithCheckBox_seekbarWithCheckBox_max, 100));
        final int multiple = ta.getInt(R.styleable.SeekbarWithCheckBox_seekbarWithCheckBox_multiple, 1);
        seekBar.setProgress(ta.getInteger(R.styleable.SeekbarWithCheckBox_seekbarWithCheckBox_progress, 0));
        int progress = seekBar.getProgress();
        String formattedProgress;
        if (progress == 0) {
            formattedProgress = "0.0";
        } else {
            formattedProgress = String.valueOf(seekBar.getProgress() / 1.0f / multiple);
        }
        processTextView.setText(formattedProgress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 当进度改变时，更新TextView的文本
                float decimalProgress = progress / 1.0f / multiple;
                processTextView.setText(String.valueOf(decimalProgress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 开始拖动SeekBar时的回调
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 停止拖动SeekBar时的回调
            }
        });

        // 设置CheckBox的OnCheckedChangeListener
        checkChangeListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    seekBar.setEnabled(false);
                    seekBar.setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_progress_follow));
                    seekBar.setThumb(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_thumb_follow));
                    seekBar.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            return true; // 返回 true 消耗触摸事件，阻止拖动
                        }
                    });
                } else {
                    seekBar.setProgressDrawable(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_progress));
                    seekBar.setThumb(ContextCompat.getDrawable(context, R.drawable.custom_seekbar_thumb));
                    seekBar.setEnabled(true);
                    seekBar.setOnTouchListener(null); // 移除OnTouchListener
                }
            }
        };

        checkBox.setOnCheckedChangeListener(checkChangeListener);

        ta.recycle();


    }

    public void setProgressValue(int progress) {
        seekBar.setProgress(progress);
    }
}
